package com.zhu.service.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhu.model.pojo.model.system.SysMenu;

import com.zhu.model.pojo.model.system.SysRoleMenu;
import com.zhu.model.pojo.model.system.vo.AssginMenuVo;
import com.zhu.service.system.mapper.MenuMapper;
import com.zhu.service.system.mapper.RoleMenuMapper;
import com.zhu.service.system.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhu.service.util.handler.utils.MenuHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author zhu
 * @since 2022-10-30
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, SysMenu> implements IMenuService {
    @Autowired
    private MenuMapper menuMapper;

    /**
     * 查询菜单列表（tree）
     * @return
     */
    @Override
    public List<SysMenu> findNodes() {
        //1.获取所有菜单
        List<SysMenu> sysMenuList = menuMapper.selectList(null);
        //2. 递归获取菜单
        List<SysMenu> resultList= MenuHelper.buildTree(sysMenuList);
        return resultList;
    }

    @Override
    public List<SysMenu> findNodes2() {
        List<SysMenu> list = menuMapper.findNodes2();
        return list;
    }

    /**
     * 根据id删除菜单
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> removeMenuById(String id) {
        Map<String, Object> map = new HashMap<>();
        //判断此菜单之下是否存在子菜单
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysMenu::getParentId,id);
        Integer count = menuMapper.selectCount(queryWrapper);
        if(count>0){
            map.put("message","存在子菜单不能删除！");
            return map;
        }
        menuMapper.deleteById(id);
        map.put("message","删除成功！");
        return map;
    }
//    ==========================================================角色菜单==========================================
@Autowired
private RoleMenuMapper roleMenuMapper;
    /**
     * 根据角色id查询菜单
     * @param roleId
     * @return
     */
    @Override
    public List<SysMenu> findMenuByRoleId(String roleId) {
        //获取所有菜单列表 status=1
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysMenu::getStatus,1);
        List<SysMenu> menuList = baseMapper.selectList(queryWrapper);
        //根据角色id查询角色分配的菜单列表
        LambdaQueryWrapper<SysRoleMenu> roleMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roleMenuLambdaQueryWrapper.eq(SysRoleMenu::getRoleId,roleId);
        List<SysRoleMenu> sysRoleMenuList = roleMenuMapper.selectList(roleMenuLambdaQueryWrapper);

        List<String> menuIdList=sysRoleMenuList.stream().map(sysRoleMenu ->
                sysRoleMenu.getMenuId()
                ).collect(Collectors.toList());
        //获取角色分配的菜单id
        //数据处理 isSelect 如果选中则为true
        for (SysMenu sysMenu : menuList) {
            if(menuIdList.contains(sysMenu.getId())){
                sysMenu.setSelect(true);
            }else {
                sysMenu.setSelect(false);
            }
        }
        List<SysMenu> menuListFinal = MenuHelper.buildTree(menuList);
        return  menuListFinal;
    }

    /**
     * 给角色分配菜单
     * @param assginMenuVo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void doAssign(AssginMenuVo assginMenuVo) {
        //删除用户原本的菜单列表
        LambdaQueryWrapper<SysRoleMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRoleMenu::getRoleId,assginMenuVo.getRoleId());
        roleMenuMapper.delete(queryWrapper);

        //遍历菜单id列表，添加
        List<String> menuIdList = assginMenuVo.getMenuIdList();
        menuIdList.forEach(menuId->{
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setRoleId(assginMenuVo.getRoleId());
            sysRoleMenu.setMenuId(menuId);
            roleMenuMapper.insert(sysRoleMenu);
        });
    }
}
